const express = require("express");
const router = express.Router();
const db = require('../../db/index');
const jwt = require("jsonwebtoken");//token
const bcrypt = require("bcryptjs");//加密密码
// 登录
router.post('/login', (req, res) => {
    let { username, password, code } = req.body;
    let { captcha, captchaTime } = req.session;
    const currentTime = Date.now();//当前时间
    if (!captcha) {
        res.send({ code: 0, data: null, msg: '验证码已失效' });
        return;
    }
    if (code.toUpperCase() == captcha.toUpperCase() && currentTime - captchaTime > 60000) {
        res.send({ code: 0, data: null, msg: '验证码已失效' });
        return;
    }
    if (code.toUpperCase() != captcha.toUpperCase()) {
        res.send({ code: 0, data: null, msg: '验证码不正确' });
        return;
    }
    db.query('select * from user where username = ?', [username], (err, data) => {
        if (err) err;
        if (data.length > 0) {
            if (bcrypt.compareSync(password, data[0].password)) {     //compareSync(当前输入密码,数据库里的密码)为true
                delete data[0].password;//剔除密码,不返回
                req.session.cunToken = jwt.sign({ username }, 'zhaoxudong');//存储生成的token
                res.send({ code: 200, data: data[0], token: jwt.sign({ username }, 'zhaoxudong'), msg: '登录成功' });//token
            } else {
                res.send({ code: 0, data: null, msg: '账号密码不正确' });
            }
        } else {
            res.send({ code: 0, data: null, msg: '用户不存在' });
        }
    });
});
// 角色权限菜单
router.get('/roleMenu', (req, res) => {
  
    if (req.session.cunToken!=req.headers.token) {
        res.send({ code: 0, data: null, msg: 'token失效' });
        return;
    }
    let { userid } = req.query;
    db.query('select * from rolemenu where userid = ?', [userid], (err, data) => {
        if (err) { err; };
        if (data.length < 1) {
            res.send({ code: 0, msg: '获取角色菜单' });
        }
        
        for (let i = 0; i < data.length; i++) {
            // if (data[i].component=='') {
            //     delete data[i].component
            // }
            if (data[i].isRouter=='1') {
                data[i].isRouter=true 
            } else {
                data[i].isRouter=false
            }
            if (data[i].hidden=='1') {
                data[i].hidden=true 
            } else {
                data[i].hidden=false
            }
            if (data[i].buffer=='1') {
                data[i].buffer=true 
            } else {
                data[i].buffer=false
            }
            if (data[i].sireId !== null) {
                const parentId = data[i].sireId;
                const parentIndex = data.findIndex(item => item.routerid === parentId);
                if (parentIndex !== -1) {
                    if (!data[parentIndex].children) {
                        data[parentIndex].children = [];
                    }
                    data[parentIndex].children.push(data[i]);
                    data.splice(i, 1);
                    i--;
                }
            }
        }
        console.log(data,'data');
        res.send({ code: 200, data: data, msg: '获取角色菜单成功' });
    });
});
module.exports = router;